<?php include('header.php');
use \PFT\Db;
$aid = $_SESSION['sid'];
if (!$aid) {
    exit('Permission Denied!');
}
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';
include '/var/www/html/new/d/module/common/Db.class.php';
$sql = "SELECT * FROM pft_wxshops WHERE aid=?";
Db::Conf($dbConf['remote_1']);
$db_remote = Db::Connect();
$stmt = $db_remote->prepare($sql);
$stmt->execute(array($_SESSION['sid']));
$shops_list = $shop_fans_cnt = array();
while ($row = $shop_info = $stmt->fetch(PDO::FETCH_ASSOC) ) {
    $shops_list[$row['id']] = $row;
    $fid_list[] = $row['fid'];
}
//统计通过该分店关注人数
$sql_fans   = "SELECT COUNT(*) as cnt,shop_id FROM pft_wxshop_fans WHERE shop_id IN("
    .implode(',', array_keys($shops_list)).") GROUP by shop_id";
$stmt       = $db_remote->prepare($sql_fans);
$stmt->execute(array($shop_info['id']));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $shop_fans_cnt[$row['shop_id']] = $row['cnt'];
}

//我的分销商
Db::Conf($dbConf['localhost']);
$sql =  <<<SQL
SELECT son_id FROM pft_member_relationship
WHERE parent_id=? AND son_id_type=0 AND status=0
SQL;
$stmt = Db::Connect()->prepare($sql);
$stmt->execute(array($_SESSION['sid']));
$son_id_list = $stmt->fetchAll(PDO::FETCH_COLUMN);
if ($son_id_list) {
    $son_id_list = implode(',', $son_id_list);
    $sql_my_dist = "SELECT id,dname FROM pft_member WHERE id IN($son_id_list)";
    $stmt = Db::Connect()->prepare($sql_my_dist);
    $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $dist_list[$row['id']] = $row['dname'];
    }
}
?>
<style>
    #accountDetailUl li{
        list-style: none;
        height: 60px;
    }
    .qr-img{
        width: 60px;
        height: 60px;;
    }
</style>
<title>微信分店配置</title>
</head>
<body>
	<div class="navbar navbar-inverse navbar-static-top" role="navigation" style="position:static;">
		<div class="container-fluid">
			<ul class="nav navbar-nav">
				<li><a href="http://www.12301.cc"><i class="fa fa-reply-all"></i>返回系统</a></li>
			</ul>
		</div>
	</div>
	<div class="container-fluid">
		<div class="row">
            <?php include('leftbar.php') ?>
            <div class="col-xs-12 col-sm-9 col-lg-10">
                <iframe src="about:blank" name="_hide_frame" id="_hide_frame"
                        style="height:0;width:0;display: none;" frameborder="0"></iframe>
                <form class="form-inline" action="wx_shop_qr.php" method="post" target="_hide_frame">
                    <div class="form-group">
                        <label for="exampleInputName2">分店简称</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="不能超过四个汉字">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail2">分销商</label>
                        <select name="fid" class="form-control">
                            <option value="0">--选择--</option>
                            <?php foreach($dist_list as $fid=>$dname):?>
                                <option value="<?=$fid?>"><?=$dname?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
<!--                    <button type="search" class="btn btn-default">查找</button>-->
                    <button type="submit" class="btn btn-primary">添加</button>
                </form>
                <br/><br/>
                <?php if(count($shops_list)):?>
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>分店编号</th>
                                <th>分销商</th>
                                <th>分店简称</th>
                                <th>二维码（下载请右键“另存为”）</th>
                                <th>粉丝数</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach($shops_list as $key=>$shop):?>
                            <tr>
                                <td><?=$shop['id']?></td>
                                <td><?=$dist_list[$shop['fid']]?>（<?=$shop['fid']?>）</td>
                                <td><?=$shop['name']?></td>
                                <td>
                                    <a title="点击查看大图" href="<?=$shop['qr_path']?>" target="_blank">
                                        <img class="qr-img" src="<?=$shop['qr_path']?>"/>
                                    </a>
                                </td>
                                <td><?=$shop_fans_cnt[$shop['id']]?></td>
                                <td>
<!--                                    <a class="btn-del" href="javascript:;">删除</a>-->
                                </td>
                            </tr>
                        <?php endforeach;?>
                        </tbody>
                    </table>
                <?php else:?>
                <div class="panel panel-default">
                    <div id="account-panel-body" class="panel-body">
                        您还没有添加过分店
                    </div>
                </div>
                <?php endif;?>
            </div>
		</div>
	</div>
	<div style="display:none" id="gMaskLayer" class="gMaskLayer"></div>
    <script>

    </script>
</body>